package api

import (
	"net/http"
	"3d-space/internal/database"

	"github.com/gin-gonic/gin"
)

type CommentRequest struct {
	Content string `json:"content" binding:"required"`
	IsVoice bool   `json:"is_voice"`
}

func AddComment(c *gin.Context) {
	userID := c.GetInt64("user_id")
	modelID := c.Param("id")

	var req CommentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := database.DB.Exec(
		"INSERT INTO comments (model_id, user_id, content, is_voice) VALUES (?, ?, ?, ?)",
		modelID, userID, req.Content, req.IsVoice,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "添加留言失败"})
		return
	}

	commentID, _ := result.LastInsertId()
	c.JSON(http.StatusCreated, gin.H{"comment_id": commentID})
}
